# coding=utf-8
from .page_base import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

class LoginPage(BasePage):
    
    def __init__(self,username,password,driver=None):
        super(LoginPage,self).__init__(driver)
        self.username = username
        self.password = password
        
    
    @property
    def username_input_element(self):
        return self.find_element(By.ID, "userName")
    
    @username_input_element.setter
    def set_username(self,username):
        self.username_input_element.send_keys(username)
        
    @property
    def password_input_element(self):
        # WebDriverWait(self.driver,3).until(EC.visibility_of_element_located((By.ID,"password")))
        return self.find_element(By.ID, "password")
    
    @password_input_element.setter
    def set_password(self,password):
        self.password_input_element.send_keys(password)
    
    @property
    def login_button_element(self):
        # WebDriverWait(self.driver,3).until(EC.element_to_be_clickable((By.XPATH,"//button[@type='submit']")))
        return self.driver.find_element(By.XPATH,"//button[@type='submit']")
    
    @property
    def login_button_element(self):
        # WebDriverWait(self.driver,3).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="app"]/div/div/div/main/div/div[2]/form/div[3]/div/button/span[2]')))
        return self.find_element(By.XPATH,'//*[@id="app"]/div/div/div/main/div/div[2]/form/div[3]/div/button/span[2]')
    
    @property
    def login_fail_prompt(self):
        # WebDriverWait(self.driver,3).until(EC.visibility_of_element_located((By.XPATH,"/html/body/div[2]/div/div/div/span[2]")))
        return  self.find_element(By.XPATH,"/html/body/div[2]/div/div/div/span[2]")
    
    def login(self,type=0):
        '''
        type 1 登录失败 None 登录成功
        '''
        # 显式等待
        WebDriverWait(self.driver,3).until(
            EC.element_to_be_clickable((By.XPATH,"//button[@type='submit']"))
        )
        self.set_username = self.username
        self.set_password = self.password
        self.login_button_element.click()
        
        # 用户头像出现
        # WebDriverWait(self.driver,3).until(
        #     EC.visibility_of_element_located((By.XPATH,"/html/body/div/div/div/header/div/div/img"))
        # )
        if (type == 1):
            WebDriverWait(self.driver,3).until(EC.visibility_of_element_located((By.XPATH,"/html/body/div[2]/div/div/div/span[2]")))